package com.key.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

public class XspMessage {

	public static String ERROR = "1";
	public static String INFORMATION = "2";
	public static String WARNING = "3";

	private String title;
	private String content;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * 设置
	 * 
	 * @param title
	 * @param content
	 */
	public String setMessage(String title, String content, String style) {
		String message = "Success";
		HttpServletRequest request = ServletActionContext.getRequest();
		if (style == null) {
			message = "Success";
		} else if (style.equals(XspMessage.ERROR)) {
			message = "Failure";
		} else if (style.equals(XspMessage.INFORMATION)) {
			message = "Infomation";
		} else if (style.equals(XspMessage.WARNING)) {
			message = "Warning";
		}
		this.setTitle(title);
		this.setContent(content);
		request.setAttribute("xspMessage", this);
		return message;
	}

	/**
	 * 设置
	 * 
	 * @param title
	 * @param content
	 */
	public String setMessage(String title, Exception e, String style) {
		String message = "Success";
		String content = e.toString();
		StackTraceElement[] stack = e.getStackTrace();
		StringBuilder sk = new StringBuilder();
		for (int i = 0; i < stack.length; i++) {
			sk.append(stack[i].toString());
			sk.append("\n");
		}
		content = sk.toString();
		HttpServletRequest request = ServletActionContext.getRequest();
		if (style == null) {
			message = "Success";
		} else if (style.equals(XspMessage.ERROR)) {
			message = "Failure";
		} else if (style.equals(XspMessage.INFORMATION)) {
			message = "Infomation";
		} else if (style.equals(XspMessage.WARNING)) {
			message = "Warning";
		}
		this.setTitle(title);
		this.setContent(content);
		request.setAttribute("xspMessage", this);
		return message;
	}
}
